<?php

use yii\widgets\LinkPager;
use yii\web\View;

$css=<<<CSS
    .container{
        width:95% !important;
    }
    .tongji-box{
        margin-bottom:10px;
    }
    .tongji-box button{
        margin-right:10px;
        margin-bottom:5px; 
    }
CSS;

$js=<<<JS
    
    var tongji = {};
    $("#duli table tr").each(function(i, n){
        var type = $(n).find("td").eq(3).text();
        var money = parseFloat($(n).find("td").eq(4).text());
        var tmp = {};

        if ($.isEmptyObject(tongji)) {
            tmp.type = type;
            tmp.num =  1;
            tmp.money =  money;
            tongji[i] =  tmp;
        } else {

            var isexist = false;
            $.each(tongji,function(i,n){ 
                if (type == n.type) {
                    isexist = true;
                    tongji[i].num = n.num + 1;
                    tongji[i].money = n.money + money;
                    return true;
                }
            });
            
            if (!isexist) {
                tmp.type = type;
                tmp.num =  1;
                tmp.money =  money;
                tongji[i] =  tmp; 
            }
        }
    });
  
    var duli_html  = '';
    var total = 0;
    $.each(tongji,function(i,n){ 
        if (n.type) {
            duli_html += "<button class='btn btn-primary' type='button'><span>"+n.type+"</span><span class='badge'>"+n.num+"</span> -- <span class='badge'>"+n.money+"</span></button>";  
            total += n.money;
        }
    });

    duli_html += "<button class='btn btn-primary' type='button'><span>总金额</span><span class='badge'>"+total+"</span></button>";
    $("#duli_tongji").html(duli_html);

    var tongji = {};  
    $("#master table tr").each(function(i, n){
        var type = $(n).find("td").eq(4).text();
        var money = parseFloat($(n).find("td").eq(3).text());
        var tmp = {};

        if ($.isEmptyObject(tongji)) {
            tmp.type = type;
            tmp.num =  1;
            tmp.money =  money;
            tongji[i] =  tmp;
        } else {

            var isexist = false;
            $.each(tongji,function(i,n){ 
                if (type == n.type) {
                    isexist = true;
                    tongji[i].num = n.num + 1;
                    tongji[i].money = n.money + money;
                    return true;
                }
            });
            
            if (!isexist) {
                tmp.type = type;
                tmp.num =  1;
                tmp.money =  money;
                tongji[i] =  tmp; 
            }
        }
    });

        
    var duli_html  = '';
    var total = 0;
    $.each(tongji,function(i,n){ 

        if (n.type) {
            duli_html += "<button class='btn btn-primary' type='button'><span>"+n.type+"</span> <span class='badge'>"+n.num+"</span>--<span class='badge'>"+n.money+"</span></button>";  
            total += n.money;
        }

    });

    duli_html += "<button class='btn btn-primary' type='button'><span>总金额</span><span class='badge'>"+total+"</span></button>";
    $("#master_tongji").html(duli_html);
    
    //高级查找
    $("#duli_tongji").on("click", "button", function(){
    
            $("tr").removeClass("bg-success");
            $("tr").removeClass("bg-danger");

            var compare = 0; //0 一样多  1，独立多余总的  2，独立的小于总的

            //独立
            var type = $(this).find('span').eq(0).text();
            var num = parseInt($(this).find('span').eq(1).text());

            //master
            var isMasterExists = false;
            var mobj = $("#master_tongji").find("button span:contains('"+type+"')");
            if (mobj) {
                isMasterExists = true;
                mobjNum = parseInt(mobj.next("span").text());
            }

            if (isMasterExists) {
                if (num > mobjNum) {
                    compare = 1;
                } else if (num < mobjNum) {
                    compare = 2;
                }
            }

           compares(type);
    });

    function compares(searchtype){
        
        var duliObj = {};
        var masterObj = {};

         $("#duli table tr").each(function(i, n){
            var type = $(n).find("td").eq(3).text();
            if (type == searchtype) {
                
                duliObj[i] = {
                    phone:$(n).find("td").eq(2).text(),
                    money:$(n).find("td").eq(4).text(),
                    adminuser:$(n).find("td").eq(1).text(),
                    createtime:$(n).find("td").eq(6).text().substring(0,16)
                };

                $(n).addClass('bg-danger');
            }
        
        });
    
        $("#master table tr").each(function(i, n){
            var type = $(n).find("td").eq(4).text();
            if (type == searchtype) {
            masterObj[i] = {
                    phone:$(n).find("td").eq(5).text(),
                    money:$(n).find("td").eq(3).text(),
                    adminuser:$(n).find("td").eq(6).text(),
                    createtime:$(n).find("td").eq(1).text().substring(0,16)
                };
                $(n).addClass('bg-danger');
            }
        });

         $.each(duliObj,function(i,n){
            $.each(masterObj,function(ii,nn){
                if (n.phone == nn.phone && n.money == nn.money && n.adminuser == nn.adminuser && n.createtime == nn.createtime) {
                    $("#duli table tr").eq(i).removeClass("bg-danger").addClass('bg-success');
                    $("#master table tr").eq(ii).removeClass("bg-danger").addClass('bg-success');
                }
            })

        });
    }

JS;

$this->registerCss($css);
$this->registerJs($js);
?>
<div>

<div class="row">
    <div class="col-md-12">
        
        <?php 
        echo LinkPager::widget([
            'pagination' => $pagination,
        ]);
        ?>
    
    </div>
</div>

<div class="row no-padding">
    <div class="col-md-6" id="duli">
    
    <div class="panel panel-primary">
        <div class="panel-heading">独立[<?= $duli['total']['count'] ?>]</div>
        <div class="panel-body">
        
        <div class="col-md-12 tongji-box" id="duli_tongji"></div>
        
            <table class="table table-bordered">
            <thead>
                <tr>
                    <th>序号--id</th>
                    <th>商户--用户id</th>
                    <th>用户电话</th>
                    <th>消费名称</th>
                    <th>消费金额</th>
                    <th>type</th>
                    <th>消费时间</th>
                    <th>商户余额</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                    if (!empty($duli['data'])):
                    foreach ($duli['data'] as $key => $item):
                ?>
                <tr>
                    <td><?= $key.'--'.$item['id']?></td>
                    <td><?= $item['userid'].'--'.$item['belongid']?></td>
                    <td><?= $item['phone']?></td>
                    <td><?= $item['message']?></td>
                    <td><?= $item['expense']?></td>
                    <td><?= $item['type']?></td>
                    <td><?= $item['time']?></td>
                    <td><?= $item['user_balance']?></td>
                </tr>
                
                <?php 
                    endforeach;
                    endif;
                ?>
            </tbody>
            </table>
        </div>
    </div>
      
    

    </div>
    
    <div class="col-md-6" id="master">
        <div class="panel panel-primary">
        <div class="panel-heading">总库[<?= $master['total']['count'] ?>]</div>
        <div class="panel-body">
            <div class="col-md-12 tongji-box" id="master_tongji"></div>
            <table class="table table-bordered">
            <thead>
                <tr>
                    <th>商户余额</th>
                    <th>消费时间</th>
                    <th>type</th>
                    <th>消费金额</th>
                    <th>消费名称</th>
                    <th>用户电话</th>
                    <th>商户--用户id</th>
                    <th>序号--id</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                    if (!empty($master['data'])):
                    foreach ($master['data'] as $key => $item):
                ?>
                <tr>
                	<td><?= $item['user_balance']?></td>
                	<td><?= $item['time']?></td>
                	<td><?= $item['type']?></td>
                	<td><?= $item['expense']?></td>
                	<td><?= $item['message']?></td>
                	<td><?= $item['phone']?></td>
                	<td><?= $item['userid'].'--'.$item['belongid']?></td>
                    <td><?= $key.'--'.$item['id']?></td> 
                </tr>
                
                <?php 
                    endforeach;
                    endif;
                ?>
            </tbody>
            </table>
        </div>
        
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        
        <?php 
        echo LinkPager::widget([
            'pagination' => $pagination,
        ]);
        ?>
    
    </div>
</div>
</div>

